package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-16 17:35
 **/
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void upload(ArrayList<OrderSetting> dataList) {
        orderSettingDao.upload(dataList);
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            // 存在当前日期
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            // 不存在当前日期
            orderSettingDao.add(orderSetting);

        }
    }

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String data) {
        // 获取第一天日期
        String begin = data + "-01";
        // 获取最后一天日期
        String end = DateUtils.getLastDayOfMonth(data);

        // 用map传入参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);

        // 然后转换数据
        ArrayList<Map<String, Object>> dataList = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            // this.leftobj = [
            //     { date: 1, number: 120, reservations: 1 },
            //     { date: 3, number: 120, reservations: 1 },
            //     { date: 4, number: 120, reservations: 120 },
            //     { date: 6, number: 120, reservations: 1 },
            //     { date: 8, number: 120, reservations: 1 }
            // ];
            HashMap<String, Object> hashMap = new HashMap<>();

            hashMap.put("date", orderSetting.getOrderDate().getDate());
            hashMap.put("number", orderSetting.getNumber());
            hashMap.put("reservations", orderSetting.getReservations());

            dataList.add(hashMap);
        }
        return dataList;
    }
}
